//
//  YFShopRegisterVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFShopRegisterVC.h"
#import "YFTextField.h"
typedef NS_ENUM(NSInteger, ImgType){ // 将类型设置给tag, 注意tag不要重复
    ImgTypeFront = 1,           // 身份证正面
    ImgTypeBack  = 2,                // 身份证反面
    ImgTypeLifePhoto = 3,           // 生活照
    ImgTypeBusinessLicenseImg = 4,  // 营业执照照片
    ImgTypeRegisterImg = 5,         // 商标注册证或代理授权书照片
    ImgTypeFoodCirculation = 6,     // 食品流通经营许可证
    ImgTypeFoodProduction = 7,      // 食品生产许可证
};


@interface YFShopRegisterVC ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UILabel *_frontLabel;
    UILabel *_nameLabel;
    UILabel *_backLabel;
    YFTextField *_backField;
    UILabel *_lifePhotoLabel;
    YFTextField *_lifePhotoField;
    UILabel *_emailLabel;
    YFTextField *_emailFiled;
    UILabel *_addressLabel;
    YFTextField *_addressFiled;
    UILabel *_shopNameLabel;
    YFTextField *_shopNameFiled;
    UILabel *_positionLabel;
    YFTextField *_positionFiled;
    UILabel *_businessLicenseNumLabel;
    YFTextField *_businessLicenseNumFiled;
    UILabel *_businessLicenseImgLabel;
    YFTextField *_businessLicenseImgField;
    UILabel *_registerLabel;
    YFTextField *_registerField;
    UILabel *_registerImgLabel;
    YFTextField *_registerImgField;
    UILabel *_foodCirculationLabel;
    YFTextField *_foodCirculationField;
    UILabel *_foodProductionLabel;
    YFTextField *_foodProductionField;
    UILabel *_presentationLabel;
    YFTextField *_presentationField;
    UILabel *_tipLabel;
    UILabel *_idLabel;
    YFTextField *_idFiled;
     YFTextField *_frontField;
    
    YFTextField *_nameFiled;
    YFTextField *_phoneFiled;
    UIView *_bottomLine;
    UIButton *_submit;
    
    BOOL _isShowFrontImgView;
    BOOL _isShowBackImgView;
    BOOL _isShowLifePhotoImgView;
    BOOL _isShowBusinessLicenseImgView;
    BOOL _isShowRegisterImgView;
    BOOL _isShowFoodCirculationImgView;
    BOOL _isShowFoodProductionImgView;

}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *myView;

@property (nonatomic, assign) ImgType selectedImgType;

// 照片
@property (nonatomic, strong) UIImageView *frontImgView;
@property (nonatomic, strong) UIImageView *backImgView;
@property (nonatomic, strong) UIImageView *lifePhotoImgView;
@property (nonatomic, strong) UIImageView *businessLicenseImgView;
@property (nonatomic, strong) UIImageView *registerImgView;
@property (nonatomic, strong) UIImageView *foodCirculationImgView;
@property (nonatomic, strong) UIImageView *foodProductionImgView;


@end

@implementation YFShopRegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"招商入驻";
    
    _isShowFrontImgView = NO;
    _isShowBackImgView = NO;
    _isShowLifePhotoImgView = NO;
    _isShowBusinessLicenseImgView = NO;
    _isShowRegisterImgView = NO;
    _isShowFoodCirculationImgView = NO;
    _isShowFoodProductionImgView = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Height_NavBar
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor colorWithHexString:@"e7e5d9"];
    self.scrollView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar);
    self.scrollView.alwaysBounceHorizontal = NO;
    self.scrollView.scrollsToTop = YES;
    self.scrollView.scrollEnabled = YES;
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(0, 100);
    
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 100);
    self.contentView.backgroundColor = [UIColor colorWithHexString:@"e7e5d9"];
    [UIColor colorWithRGBRed:231 green:229 blue:216];
    [self.scrollView addSubview:self.contentView];
    
    [self setupView];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

- (void)setupView {
    
    CGFloat padding = 10;
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"banner5"];
    imgView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 180);
    [self.contentView addSubview:imgView];
    
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.text = @"泉界商城线上产品入驻申请";
    label1.font = [UIFont systemFontOfSize:17];
    label1.frame = CGRectMake(padding, CGRectGetMaxY(imgView.frame), UISCREEN_WIDTH-2*padding, 40);
    label1.textColor = YFColor_Text;
    [self.contentView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"       泉界商城线上产品入驻平台申请入口，填写对应信息，工作人员会在5个工作日内给您电话回访，感恩有你。";
    label2.numberOfLines = 0;
    label2.font = [UIFont systemFontOfSize:14];
    label2.frame = CGRectMake(padding, CGRectGetMaxY(label1.frame), UISCREEN_WIDTH-2*padding, 60);
    label2.textColor = [UIColor colorWithHexString:@"666666"];
    [self.contentView addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.text = @"注：为了方便您尽快的免费入驻，请完整填写一下内容，并确保您提供资料信息准确无误。";
    label3.numberOfLines = 0;
    label3.font = [UIFont systemFontOfSize:12];
    label3.textColor = YFColor_Red;
    label3.frame = CGRectMake(padding, CGRectGetMaxY(label2.frame), UISCREEN_WIDTH-2*padding, 50);
    [self.contentView addSubview:label3];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    line.frame = CGRectMake(0, CGRectGetMaxY(label3.frame), UISCREEN_WIDTH, 1);
    [self.contentView addSubview:line];
    
    /*********************   输入信息    *****************************/
    CGFloat labelH = 40;
    CGFloat filedH = 30;
    
    // 名字
    UILabel *nameLabel = [self createLabel];
    nameLabel.attributedText = [self changeLastColorWithStr:@"负责人姓名*"];;
    nameLabel.frame =  [self frameLabelWithMaxY:CGRectGetMaxY(line.frame)];
    _nameLabel = nameLabel;
    
    YFTextField *nameFiled = [self createTextField];
    nameFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(nameLabel.frame)];
    _nameFiled = nameFiled;
    
    // 手机号
    UILabel *phoneLabel = [self createLabel];
    phoneLabel.attributedText = [self changeLastColorWithStr:@"负责人手机号码*"];
    phoneLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(nameFiled.frame)];
    
    YFTextField *phoneFiled = [self createTextField];
    phoneFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(phoneLabel.frame)];
    _phoneFiled = phoneFiled;
    // 身份证
    UILabel *idLabel = [self createLabel];
    idLabel.text = @"法人代表身份证号码";
    idLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(phoneFiled.frame)];
    _idLabel = idLabel;
    
    YFTextField *idFiled = [self createTextField];
    idFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(idLabel.frame)];
    _idFiled = idFiled;
    
    // 照片 Front and back
    UILabel *frontLabel = [self createLabel];
    frontLabel.text = @"法人代表身份证正面照片";
    frontLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(idFiled.frame)];
    _frontLabel = frontLabel;
    
    YFTextField *frontField = [self createUploadImgField];
    frontField.tag = ImgTypeFront;
    _frontField = frontField;
    frontField.frame = [self frameImgWithMaxY:CGRectGetMaxY(frontLabel.frame)];

    UIImageView *frontImgView = [self createImageView];
//    frontImgView.image = [UIImage imageNamed:@"HKaidian"];
    frontImgView.backgroundColor = [UIColor whiteColor];
    self.frontImgView = frontImgView;
    frontImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(frontField.frame)];
    self.frontImgView.hidden = YES;

    
    UILabel *backLabel = [self createLabel];
    backLabel.text = @"法人代表身份证反面照片";
    _backLabel = backLabel;
    backLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(frontField.frame)];

    
    YFTextField *backField = [self createUploadImgField];
    backField.tag = ImgTypeBack;
    _backField = backField;
    backField.frame = [self frameImgWithMaxY:CGRectGetMaxY(backLabel.frame)];
    
    UIImageView *backImgView = [self createImageView];
//    backImgView.image = [UIImage imageNamed:@"HKaidian"];
    backImgView.backgroundColor = [UIColor whiteColor];
    backImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(backField.frame)];
    self.backImgView = backImgView;
    self.backImgView.hidden = YES;
    
    UILabel *lifePhotoLabel = [self createLabel];
    lifePhotoLabel.text = @"法人代表生活照照片";
    _lifePhotoLabel = lifePhotoLabel;
    lifePhotoLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(backField.frame)];

    YFTextField *lifePhotoField = [self createUploadImgField];
    lifePhotoField.tag = ImgTypeLifePhoto;
    _lifePhotoField = lifePhotoField;
    lifePhotoField.frame = [self frameImgWithMaxY:CGRectGetMaxY(lifePhotoLabel.frame)];
    
    UIImageView *lifePhotoImgView = [self createImageView];
//    lifePhotoImgView.image = [UIImage imageNamed:@"HKaidian"];
    lifePhotoImgView.backgroundColor = [UIColor whiteColor];
    lifePhotoImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(lifePhotoField.frame)];
    self.lifePhotoImgView = lifePhotoImgView;
    self.lifePhotoImgView.hidden = YES;
    // 邮箱
    UILabel *emailLabel = [self createLabel];
    emailLabel.attributedText = [self changeLastColorWithStr:@"常用邮箱*"];
    _emailLabel = emailLabel;
    emailLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(lifePhotoField.frame)];

    YFTextField *emailFiled = [self createTextField];
    _emailFiled = emailFiled;
    emailFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(emailLabel.frame)];

    // 地址
    UILabel *addressLabel = [self createLabel];
    addressLabel.attributedText = [self changeLastColorWithStr:@"联系地址*"];
    _addressLabel = addressLabel;
    addressLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(emailFiled.frame)];

    YFTextField *addressFiled = [self createTextField];
    _addressFiled = addressFiled;
    addressFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(addressLabel.frame)];
    
    // 企业名字
    UILabel *shopNameLabel = [self createLabel];
    shopNameLabel.text = @"企业名字";
    _shopNameLabel = shopNameLabel;
    shopNameLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(addressFiled.frame)];

    YFTextField *shopNameFiled = [self createTextField];
    _shopNameFiled = shopNameFiled;
    shopNameFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(shopNameLabel.frame)];
    
    // 负责人职位
    UILabel *positionLabel = [self createLabel];
    positionLabel.text = @"负责人职位";
    _positionLabel = positionLabel;
    positionLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(shopNameFiled.frame)];

    YFTextField *positionFiled = [self createTextField];
    _positionFiled = positionFiled;
    positionFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(positionLabel.frame)];

    
    // 营业执照号码
    UILabel *businessLicenseNumLabel = [self createLabel];
    businessLicenseNumLabel.text = @"营业执照号码";
    _businessLicenseNumLabel = businessLicenseNumLabel;
    businessLicenseNumLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(positionFiled.frame)];

    YFTextField *businessLicenseNumFiled = [self createTextField];
    _businessLicenseNumFiled = businessLicenseNumFiled;
    businessLicenseNumFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(businessLicenseNumLabel.frame)];

    // 营业执照照片
    UILabel *businessLicenseImgLabel = [self createLabel];
    businessLicenseImgLabel.text = @"营业执照照片";
    _businessLicenseImgLabel = businessLicenseImgLabel;
    businessLicenseImgLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(businessLicenseNumFiled.frame)];

    YFTextField *businessLicenseImgField = [self createUploadImgField];
    businessLicenseImgField.tag = ImgTypeBusinessLicenseImg;
    _businessLicenseImgField = businessLicenseImgField;
    businessLicenseImgField.frame = [self frameImgWithMaxY:CGRectGetMaxY(businessLicenseImgLabel.frame)];

    UIImageView *businessLicenseImgView = [self createImageView];
//    businessLicenseImgView.image = [UIImage imageNamed:@"HKaidian"];
    businessLicenseImgView.backgroundColor = [UIColor whiteColor];
    businessLicenseImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(businessLicenseImgField.frame)];
    self.businessLicenseImgView = businessLicenseImgView;
    self.businessLicenseImgView.hidden = YES;
    
    // 商标注册证或代理授权书号码
    UILabel *registerLabel = [self createLabel];
    registerLabel.text = @"商标注册证或代理授权书号码";
    _registerLabel = registerLabel;
    registerLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(businessLicenseImgField.frame)];

    YFTextField *registerField = [self createTextField];
    _registerField = registerField;
    registerField.frame = [self frameFieldWithMaxY:CGRectGetMaxY(registerLabel.frame)];

    // 商标注册证或代理授权书照片
    UILabel *registerImgLabel = [self createLabel];
    registerImgLabel.text = @"商标注册证或代理授权书照片";
    _registerImgLabel = registerImgLabel;
    registerImgLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(registerField.frame)];

    YFTextField *registerImgField = [self createUploadImgField];
    registerImgField.tag = ImgTypeRegisterImg;
    _registerImgField = registerImgField;
    registerImgField.frame = [self frameImgWithMaxY:CGRectGetMaxY(registerImgLabel.frame)];
    
    UIImageView *registerImgView = [self createImageView];
//    registerImgView.image = [UIImage imageNamed:@"HKaidian"];
    registerImgView.backgroundColor = [UIColor whiteColor];
    registerImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(registerImgField.frame)];
    self.registerImgView = registerImgView;
    self.registerImgView.hidden = YES;
    
    // 食品流通经营许可证
    UILabel *foodCirculationLabel = [self createLabel];
    foodCirculationLabel.text = @"非生产商要求食品流通许可证或食品流通经营许可证";
    _foodCirculationLabel = foodCirculationLabel;
    foodCirculationLabel.numberOfLines = 0;
    foodCirculationLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(registerImgField.frame) height:60];

    YFTextField *foodCirculationField = [self createUploadImgField];
    foodCirculationField.tag = ImgTypeFoodCirculation;
    _foodCirculationField = foodCirculationField;
    foodCirculationField.frame = [self frameImgWithMaxY:CGRectGetMaxY(foodCirculationLabel.frame)];
    
    UIImageView *foodCirculationImgView = [self createImageView];
//    foodCirculationImgView.image = [UIImage imageNamed:@"HKaidian"];
    foodCirculationImgView.backgroundColor = [UIColor whiteColor];
    foodCirculationImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(foodCirculationField.frame)];
    self.foodCirculationImgView = foodCirculationImgView;
    self.foodCirculationImgView.hidden = YES;
    
    // 食品生产许可证
    UILabel *foodProductionLabel = [self createLabel];
    foodProductionLabel.text = @"生产商要求全国工业品生产许可证或食品生产许可证";
    _foodProductionLabel = foodProductionLabel;
    foodProductionLabel.numberOfLines = 0;
    foodProductionLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(foodCirculationField.frame) height:60];

    YFTextField *foodProductionField = [self createUploadImgField];
    foodProductionField.tag = ImgTypeFoodProduction;
    _foodProductionField = foodProductionField;
    foodProductionField.frame = [self frameImgWithMaxY:CGRectGetMaxY(foodProductionLabel.frame)];

    UIImageView *foodProductionImgView = [self createImageView];
//    foodProductionImgView.image = [UIImage imageNamed:@"HKaidian"];
    foodProductionImgView.backgroundColor = [UIColor whiteColor];
    foodProductionImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(foodProductionField.frame)];
    self.foodProductionImgView = foodProductionImgView;
    self.foodProductionImgView.hidden = YES;
    
    // 报告
    UILabel *presentationLabel = [self createLabel];
    presentationLabel.text = @"近一年产品检测报告";
    _presentationLabel = presentationLabel;
    presentationLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(foodProductionField.frame)];

    YFTextField *presentationField = [self createTextField];
    presentationField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    _presentationField = presentationField;
    presentationField.frame = CGRectMake(padding, CGRectGetMaxY(presentationLabel.frame), UISCREEN_WIDTH-2*padding, 120);

    
    // 注
    UILabel *tipLabel = [self createLabel];
    tipLabel.text = @"注:带*选项为必填选项";
    tipLabel.textColor = YFColor_Red;
    _tipLabel = tipLabel;
    tipLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(presentationField.frame)];
    
    // line
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    bottomLine.frame = CGRectMake(0, CGRectGetMaxY(tipLabel.frame) + 30, UISCREEN_WIDTH, 1);
    [self.contentView addSubview:bottomLine];
    _bottomLine = bottomLine;
    
    // 提交按钮
    UIButton *submit = [[UIButton alloc] init];
    [submit setTitle:@"提交" forState:UIControlStateNormal];
    submit.frame = CGRectMake(padding, CGRectGetMaxY(bottomLine.frame)+50, UISCREEN_WIDTH -2*padding, 44);
    submit.backgroundColor = [UIColor colorWithHexString:@"fa9811"];
    [submit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:submit];
    _submit = submit;
    
    // 重置高度
    CGFloat maxY = CGRectGetMaxY(submit.frame) + 50;
    self.scrollView.contentSize = CGSizeMake(0, maxY);
    self.contentView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, maxY);

    
//    [self resetFrame];
}
/*
 - (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
 typedef void(^ QJNetworkManagerBlock)(NSURLSessionDataTask *operetion,id response);

 */
- (void)uploadImg:(UIImage *)img success:(void (^)(NSURLSessionDataTask *operetion, id response))success failure:(void (^)(NSURLSessionDataTask *operetion, id response))failure {
    
    // @"正在提交信息..."
    [[HttpManager shareHttpManager] postImagefileWithUrl:kUploadImage parameter:nil withImageFile:img hiddenHUD:YES HUDTitle:nil success:^(NSURLSessionDataTask *operetion, id response) {
        if (response) {
            success(operetion, response);
        }
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        failure(operetion, response);
    }];
}


- (void)submitClick {
    NSLog(@"提交");
    // 有一个是NO,就不让提交了
    // 判断是否有图片, 有就上传完毕

    // 上传文字
    if (_nameFiled.text.length == 0 || _phoneFiled.text.length == 0 || _emailFiled.text.length == 0 || _addressFiled.text.length == 0) {
        
        [MBPHUD showAlertWithTitle:@"*是必填项, 请填写!"];
        return;
    }
    
    /*
     `username` 用户名
     `mobile` 手机
     `idno` 身份证号
     `idnoimg1` 身份证照片
     `idnoimg2` 身份证照片2
     `usernameimg2` 生活照
     `zip`邮编,
     `address` 地址
     `commanyname` 企业名称
     `commitname` 负责人职务
     `commanyidno` 营业执照号码
     commanyidnoimg` 营业执照照片
     `saleidno` 商品注册证号码
     `saleidnoimg` 商品注册证图片
     `commitsaleidno` 食品许可证照片
     `commitsaleidnoimg` 食品许可证图片
     `demo`  检测报告
     */

    NSDictionary *dict = @{@"username" : _nameFiled.text,//负责人姓名
                           @"mobile" : _phoneFiled.text,//手机号码
                            @"idno" : _idFiled.text,//身份证号码
                           @"idnoimg1":_frontField.text,//身份证正面
                           @"idnoimg2":_backField.text,//身份证反面
                           @"usernameimg2":_lifePhotoField.text,//生活照片
                           @"zip" : _emailFiled.text,       // 邮箱
                           @"address" : _addressFiled.text,//联系地址
                           @"commanyname" : _shopNameFiled.text,//企业名字
                           @"commitname" : _positionFiled.text,//负责人职位
                           @"commanyidno" : _businessLicenseNumFiled.text,//营业执照号码
                           @"commanyidnoimg":_businessLicenseImgField.text,//营业执照照片
                           @"saleidno" : _registerField.text,//商标注册证或代理授权书号码
                           @"commitsaleidnoimg":_registerImgLabel.text,//商标注册证或代理授权书照片
                           @"commitsaleidno": _foodCirculationField.text,//非生产商要求食品流通许可证或食品流通经营许可证
                           @"commitsaleidnoimg": _foodProductionField.text,//非生产商要求全国工业品生产许可证或食品生产许可证
                           @"demo" : _presentationField.text,//近一年生产检测报告
                           };
    
    [WWNetRequest fetchShopRegisterWithParameters:dict success:^(id response) {
        NSLog(@"========== success");
        [MBPHUD showAlertWithTitle:@"提交成功,待审核!"];
        KPopViewControllerAction;
    } failure:^(NSDictionary *error) {
        NSLog(@"========== failr");
    }];

}


- (NSMutableAttributedString *)changeLastColorWithStr:(NSString *)str{
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
    NSRange colorRange = NSMakeRange(str.length-1, 1);
    [attStr addAttribute:NSForegroundColorAttributeName value:YFColor_Red range:(colorRange)];
    return attStr;
}

- (NSAttributedString *)labelWithColor:(UIColor *)color location:(NSUInteger)loc colorText:(NSString *)colorText lable:(UILabel *)label withText:(NSString *)text
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:text];
    NSRange colorRange = NSMakeRange(loc, colorText.length);
    [str addAttribute:NSForegroundColorAttributeName value:color range:(colorRange)];
    label.attributedText = str;
    return label.attributedText;
}

- (void)resetFrame {
    /*********************   输入信息    *****************************/
    CGFloat labelH = 40;
    CGFloat filedH = 30;
    CGFloat padding = 10;
    
    // 正面
    _frontLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_idFiled.frame)];
    
    _frontField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_frontLabel.frame)];
    
    self.frontImgView.hidden = _isShowFrontImgView ? NO : YES;
    
    NSLog(@"_isShowFrontImgView = %d", _isShowFrontImgView);
    // 反面
    CGFloat backLabelY = _isShowFrontImgView ? CGRectGetMaxY(_frontImgView.frame) : CGRectGetMaxY(_frontField.frame);
    
    _backLabel.frame = [self frameLabelWithMaxY:backLabelY];
    
    _backField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_backLabel.frame)];
    
    // 重新计算frame
    self.backImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_backField.frame)];
    self.backImgView.hidden = _isShowBackImgView ? NO : YES;
    
    // 生活照
    CGFloat lifePhotoLabelY = _isShowBackImgView ? CGRectGetMaxY(_backImgView.frame) : CGRectGetMaxY(_backField.frame);
    
    _lifePhotoLabel.frame = [self frameLabelWithMaxY:lifePhotoLabelY];
    
    _lifePhotoField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_lifePhotoLabel.frame)];
    
    _lifePhotoImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_lifePhotoField.frame)];
    _lifePhotoImgView.hidden = _isShowLifePhotoImgView ? NO : YES;
    
    // 邮箱
    CGFloat emailLabelY = _isShowLifePhotoImgView ? CGRectGetMaxY(_lifePhotoImgView.frame) : CGRectGetMaxY(_lifePhotoField.frame);
    
    _emailLabel.frame = [self frameLabelWithMaxY:emailLabelY];
    
    _emailFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_emailLabel.frame)];
    
    // 地址
    _addressLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_emailFiled.frame)];
    

    _addressFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_addressLabel.frame)];
    
    // 企业名字
    _shopNameLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_addressFiled.frame)];
    
    _shopNameFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_shopNameLabel.frame)];
    
    // 负责人职位
    _positionLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_shopNameFiled.frame)];
    
    _positionFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_positionLabel.frame)];
    
    // 营业执照号码
    _businessLicenseNumLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_positionFiled.frame)];
    
    _businessLicenseNumFiled.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_businessLicenseNumLabel.frame)];
    
    // 营业执照照片
    _businessLicenseImgLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_businessLicenseNumFiled.frame)];
    
    _businessLicenseImgField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_businessLicenseImgLabel.frame)];
    
    self.businessLicenseImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_businessLicenseImgField.frame)];
    self.businessLicenseImgView.hidden = _isShowBusinessLicenseImgView?NO:YES;
    
    // 商标注册证或代理授权书号码
    CGFloat registerLabelY = _isShowBusinessLicenseImgView ? CGRectGetMaxY(_businessLicenseImgView.frame) : CGRectGetMaxY(_businessLicenseImgField.frame);
    _registerLabel.frame = [self frameLabelWithMaxY:registerLabelY];
    

    _registerField.frame = [self frameFieldWithMaxY:CGRectGetMaxY(_registerLabel.frame)];
    
    // 商标注册证或代理授权书照片
    _registerImgLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_registerField.frame)];
    
    _registerImgField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_registerImgLabel.frame)];
    
    self.registerImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_registerImgField.frame)];
    self.registerImgView.hidden = _isShowRegisterImgView?NO:YES;
    
    // 食品流通经营许可证
    CGFloat foodCirculationLabelY = _isShowRegisterImgView?CGRectGetMaxY(self.registerImgView.frame) : CGRectGetMaxY(_registerImgField.frame);
    _foodCirculationLabel.frame = [self frameLabelWithMaxY:foodCirculationLabelY height:60];
    
    _foodCirculationField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_foodCirculationLabel.frame)];
    
    self.foodCirculationImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_foodCirculationField.frame)];
    self.foodCirculationImgView.hidden = _isShowFoodCirculationImgView?NO:YES;
    
    // 食品生产许可证
    CGFloat foodProductionLabelY = _isShowFoodCirculationImgView?CGRectGetMaxY(self.foodCirculationImgView.frame):CGRectGetMaxY(_foodCirculationField.frame);
    
    _foodProductionLabel.frame = [self frameLabelWithMaxY:foodProductionLabelY height:60];
    
    _foodProductionField.frame = [self frameImgWithMaxY:CGRectGetMaxY(_foodProductionLabel.frame)];
    
    self.foodProductionImgView.frame = [self frameUploadImgWithMaxY:CGRectGetMaxY(_foodProductionField.frame)];
    self.foodProductionImgView.hidden = _isShowFoodProductionImgView?NO:YES;
    
    // 报告
    CGFloat presentationLabelY = _isShowFoodProductionImgView?CGRectGetMaxY(self.foodProductionImgView.frame):CGRectGetMaxY(_foodProductionField.frame);
    
    _presentationLabel.frame = [self frameLabelWithMaxY:presentationLabelY];
    
    _presentationField.frame = CGRectMake(padding, CGRectGetMaxY(_presentationLabel.frame), UISCREEN_WIDTH-2*padding, 120);
    
    
    // 注
    _tipLabel.frame = [self frameLabelWithMaxY:CGRectGetMaxY(_presentationField.frame)];
    
    // line
    _bottomLine.frame = CGRectMake(0, CGRectGetMaxY(_tipLabel.frame) + 30, UISCREEN_WIDTH, 1);
    
    // 提交按钮
    _submit.frame = CGRectMake(padding, CGRectGetMaxY(_bottomLine.frame)+50, UISCREEN_WIDTH -2*padding, 44);
    
    // 重置高度
    CGFloat maxY = CGRectGetMaxY(_submit.frame) + 50;
    self.scrollView.contentSize = CGSizeMake(0, maxY);
    self.contentView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, maxY);
}

- (UILabel *)createLabel {
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = YFColor_Text;
    label.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:label];
    return label;
}

- (YFTextField *)createTextField {
    UIColor *borderColor = [UIColor colorWithHexString:@"dddddd"];

    YFTextField *field = [[YFTextField alloc] init];
    field.type = YFTextFieldTypeText;
    field.delegate = self;
    field.font = [UIFont systemFontOfSize:14];
    field.layer.borderColor = borderColor.CGColor;
    field.layer.borderWidth = 1;
    field.backgroundColor = [UIColor colorWithHexString:@"f5f6f0"];
    [self.contentView addSubview:field];
    return field;
}

- (YFTextField *)createUploadImgField {
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"upload"];
    imgView.frame = CGRectMake(0, 0, 22, 22);
    
    YFTextField *field = [self createTextField];
    field.type = YFTextFieldTypeUploadImg;
    field.text = @"选择照片上传";
    field.leftView = imgView;
    field.leftViewMode = UITextFieldViewModeAlways;

    return field;
}

- (UIImageView *)createImageView {
    UIImageView *imgView = [[UIImageView alloc] init];
    [self.contentView addSubview:imgView];
    return imgView;
}

- (CGRect)frameLabelWithMaxY:(CGFloat)maxY height:(CGFloat)height{
    CGFloat padding = 10;
    return CGRectMake(padding, maxY, UISCREEN_WIDTH - 2*padding, height);
}
- (CGRect)frameLabelWithMaxY:(CGFloat)maxY {
    CGFloat padding = 10;
    return CGRectMake(padding, maxY, UISCREEN_WIDTH - 2*padding, 40);
}
- (CGRect)frameFieldWithMaxY:(CGFloat)maxY {
    CGFloat padding = 10;
    return CGRectMake(padding, maxY, UISCREEN_WIDTH - 2*padding, 30);
}

- (CGRect)frameImgWithMaxY:(CGFloat)maxY {
    CGFloat padding = 10;
    return CGRectMake(padding, maxY, 307.f/750.f * UISCREEN_WIDTH, 30);
}

- (CGRect)frameUploadImgWithMaxY:(CGFloat)maxY {
    CGFloat padding = 10;
    CGFloat width = 50;
    return CGRectMake(padding, maxY, width, width);
    
}
//    return CGRectMake(padding, maxY, 500.f/750.f * UISCREEN_WIDTH, 500.f/750.f * UISCREEN_WIDTH);}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    YFTextField *field = (YFTextField *)textField;
    if (field.type == YFTextFieldTypeUploadImg) { // 上传照片
        
        // 创建field的时候就是将类型赋值给tag,这里取过来
        self.selectedImgType = textField.tag;
        [self imageUploadAction];
        return NO;
    }else if (field.type == YFTextFieldTypeText) {
        
    }

    return YES;
}

//当开始编辑文字时，（键盘弹出时）,触发此方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//    return YES;
}

//结束编辑时，(键盘收回时),触发此方法。把UITextField中的文本内容给label。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0) {
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   {
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    //返回一个BOOL值指明是否允许根据用户请求清除内容
    //可以设置在特定条件下才允许清除内容
    return YES;
}
//点击键盘右侧的按钮时，触发此方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}

#pragma mark UIImagePickerControllerDelegate
// 该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    [self dismissViewControllerAnimated:YES completion:^{

        switch (self.selectedImgType) {
            case ImgTypeFront:
                self.frontImgView.image = [image copy];
                _isShowFrontImgView = YES;
                [self uploadImageWithImage:self.frontImgView type:ImgTypeFront];
                break;
            case ImgTypeBack:
                self.backImgView.image = [image copy];
                _isShowBackImgView = YES;
                [self uploadImageWithImage:self.backImgView type:ImgTypeBack];
                break;
            case ImgTypeLifePhoto:
                self.lifePhotoImgView.image = [image copy];
                _isShowLifePhotoImgView = YES;
                 [self uploadImageWithImage:self.lifePhotoImgView type:ImgTypeLifePhoto];
                break;
            case ImgTypeBusinessLicenseImg:
                self.businessLicenseImgView.image = [image copy];
                _isShowBusinessLicenseImgView = YES;
                [self uploadImageWithImage:self.businessLicenseImgView type:ImgTypeBusinessLicenseImg];
                break;
            case ImgTypeRegisterImg:
                self.registerImgView.image = [image copy];
                _isShowRegisterImgView = YES;
                 [self uploadImageWithImage:self.registerImgView type:ImgTypeRegisterImg];
                break;
            case ImgTypeFoodCirculation:
                self.foodCirculationImgView.image = [image copy];
                _isShowFoodCirculationImgView = YES;
                 [self uploadImageWithImage:self.foodCirculationImgView type:ImgTypeFoodCirculation];
                break;
            case ImgTypeFoodProduction:
                self.foodProductionImgView.image = [image copy];
                _isShowFoodProductionImgView = YES;
                 [self uploadImageWithImage:self.foodProductionImgView type:ImgTypeFoodProduction];
                break;
        }
        // 重置界面
        [self resetFrame];
        
    }];

}

#pragma mark --上传图片
- (void)uploadImageWithImage:(UIImageView *)imgView type:(int)type{
    
    [self uploadImg:imgView.image success:^(NSURLSessionDataTask *operetion, id response) {
        
        if (response) {
            
           NSString *url =  [response objectForKey:@"url"];
            switch (self.selectedImgType) {
                case ImgTypeFront:
                    _frontField.text = url;
                    break;
                case ImgTypeBack:
                    _backField.text = url;
                    break;
                case ImgTypeLifePhoto:
                    _lifePhotoField.text = url;
                    break;
                case ImgTypeBusinessLicenseImg:
                    _businessLicenseImgField.text = url;
                    break;
                case ImgTypeRegisterImg:
                    _registerField.text = url;
                    break;
                case ImgTypeFoodCirculation:
                    _foodCirculationField.text = url;
                    break;
                case ImgTypeFoodProduction:
                    _foodProductionField.text = url;
                    break;
            }
            
        }
        
    } failure:^(NSURLSessionDataTask *operetion, id response) {
        
        
        
    }];
}

#pragma mark - private
#pragma mark -- 图片上传方法
- (void)imageUploadAction {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择图片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gotoIamgePickerWith: UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cameraAction];
    [alert addAction:photoAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}


#pragma mark -- 选择图片来源
- (void)gotoIamgePickerWith:(NSUInteger)sourceType{
    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [MBPHUD showAlertWithTitle:@"您没有对相机授权!"];
            return;
        }
    }else{
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [MBPHUD showAlertWithTitle:@"您没有对相册授权!"];
            return;
        }
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:^{
        
    }];
}

- (void)uplodaImgTest {
    NSLog(@"提交");
    
    　//此body是向后台传的参数, 因为是上传图片, 所以只给个图片名就够了, 这个和后台去问
    
    NSDictionary * body = @{@"username" : @"张三",
                            @"mobile" : @"18120557090",
                            @"zip" : @"430000",       // 邮箱
                            @"address" : @"汉阳区王家湾",
                            @"commitname" : @"经理",
                            @"idnoimg1" :@"",
                            };
    @{@"category":@"user",@"file":@"HeadImg"};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //ContentType设置
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json",nil];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    NSString *url = @"http://api.quanjieshop.com/api/index.php/home/Newtest/link_shop_add";
    [manager POST:url parameters:body constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //把image  转为data , POST上传只能传data
        
        NSData *data = UIImagePNGRepresentation([UIImage imageNamed:@"kaiian"]);
        
        //上传的参数(上传图片，以文件流的格式)
        [formData appendPartWithFileData:data
                                    name:@"idnoimg1"
                                fileName:@"kaidian.png"
                                mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //请求成功的block回调
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"上传成功%@",dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败%@",error);
        
    }];
    
    
    /*
     `username` 用户名
     `mobile` 手机
     `idno` 身份证号
     `idnoimg1` 身份证照片
     `idnoimg2` 身份证照片2
     `usernameimg2` 生活照
     `zip`邮编,
     `address` 地址
     `commanyname` 企业名称
     `commitname` 负责人职务
     `commanyidno` 营业执照号码
     commanyidnoimg` 营业执照照片
     `saleidno` 商品注册证号码
     `saleidnoimg` 商品注册证图片
     `commitsaleidno` 食品许可证照片
     `commitsaleidnoimg` 食品许可证图片
     `demo`  检测报告
     */
    NSDictionary *dict = @{@"username" : @"张三",
                           @"mobile" : @"18120557090",
                           @"zip" : @"430000",       // 邮箱
                           @"address" : @"汉阳区王家湾",
                           @"commitname" : @"经理"};
    
    [WWNetRequest fetchShopRegisterWithParameters:dict success:^(id response) {
        NSLog(@"========== success");
    } failure:^(NSDictionary *error) {
        NSLog(@"========== failr");
    }];
    
}




@end
